﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace login
{
    class DBRepository
    {
        loginModelDataContext db = new loginModelDataContext();

        public DBRepository(){

        }

        public bool adduser(String uimstring, String srmlstring){
            InitialUserInfo iui = new InitialUserInfo();
            UserInfoModel uim = new UserInfoModel();
            uim = uim.JsonToEntity(uimstring);
            iui.username = uim.Name;
            iui.gender = uim.Gender;
            iui.birth = uim.Birth;
            iui.birthplace = uim.BirthPlace;
            iui.education_level = uim.EducationLevel;
            iui.address_now = uim.AddressNow;
            iui.address_hometown = uim.AddressHometown;
            iui.health_state = uim.HealthState;
            iui.id_number = uim.IDNumber;
            iui.marriage_state = uim.MarriageState;
            iui.telephone = uim.Telephone;
            iui.policital_status = uim.PolicitalStatus;
            iui.privilege = uim.Privilege;
            iui.validate_state = 0;
            iui.right_index_finger = uim.IndexFinger;
            iui.right_middle_finger = uim.MiddleFinger;
            iui.right_ring_finger = uim.RingFinger;

            SocialRelationModel srm = new SocialRelationModel();
            List<SocialRelationModel> srml = srm.JsonToEntity(srmlstring);
            foreach(SocialRelationModel element in srml){
                SocialRelation sr = new SocialRelation();
                sr.age = element.Age;
                sr.name = element.Name;
                sr.appellation = element.Appellation;
                sr.company_and_phone = element.Company_And_Phone;
                iui.SocialRelation.Add(sr);
            }

            Photo photo = new Photo();
            photo.photo1 = uim.HeadPortrait;
            photo.photo_type = 0;    // 头像
            photo.upload_time = DateTime.Now;
            iui.Photo.Add(photo);

            db.InitialUserInfo.InsertOnSubmit(iui);
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public IQueryable<int> finduserid_byname(String name){
            var ids = from user in db.InitialUserInfo
                      where user.username == name
                      select user.id;
            return ids;
        }

        public List<byte[]> getfinger_byid(int id){
            InitialUserInfo iui = db.InitialUserInfo.Single(p => p.id == id);
            List<byte[]> lfinger = new List<byte[]>();
            lfinger.Add(iui.right_index_finger.ToArray());
            lfinger.Add(iui.right_middle_finger.ToArray());
            lfinger.Add(iui.right_ring_finger.ToArray());
            return lfinger;
        }
    }
}
